﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类message。
    /// </summary>
    public class message
    {
        public message()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_message where id=" + id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.message model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_message(");
            strSql.Append("username,tel,email,addr,title,content,addtime,ip,rel,relren,relip,reltime,zt,hits,class,pho,sex");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.username + "',");
            strSql.Append("'" + model.tel + "',");
            strSql.Append("'" + model.email + "',");
            strSql.Append("'" + model.addr + "',");
            strSql.Append("'" + model.title + "',");
            strSql.Append("'" + model.content + "',");
            strSql.Append("'" + model.addtime + "',");
            strSql.Append("'" + model.ip + "',");
            strSql.Append("'" + model.rel + "',");
            strSql.Append("'" + model.relren + "',");
            strSql.Append("'" + model.relip + "',");
            strSql.Append("'" + model.reltime + "',");
            strSql.Append("" + model.zt + ",");
            strSql.Append("" + model.hits + ",");
            strSql.Append("'" + model.Class + "',");
            strSql.Append("'" + model.pho + "',");
            strSql.Append("'" + model.sex + "'");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.message model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_message set ");
            if (model.username != null)
                strSql.Append("username='" + model.username + "',");
            if (model.tel != null)
                strSql.Append("tel='" + model.tel + "',");
            if (model.email != null)
                strSql.Append("email='" + model.email + "',");
            if (model.addr != null)
                strSql.Append("addr='" + model.addr + "',");
            if (model.title != null)
                strSql.Append("title='" + model.title + "',");
            if (model.content != null)
                strSql.Append("content='" + model.content + "',");
            if (model.ip != null)
                strSql.Append("ip='" + model.ip + "',");
            if (model.rel != null)
                strSql.Append("rel='" + model.rel + "',");
            if (model.relren != null)
                strSql.Append("relren='" + model.relren + "',");
            if (model.relip != null)
                strSql.Append("relip='" + model.relip + "',");
            if (model.reltime != null)
                strSql.Append("reltime='" + model.reltime + "',");
            if (model.zt.Equals(Int32.Parse("0")) != true)
                strSql.Append("zt=" + model.zt + ",");
            if (model.hits.Equals(Int32.Parse("0")) != true)
                strSql.Append("hits=" + model.hits + ",");
            if (model.Class != null)
                strSql.Append("class='" + model.Class + "',");
            if (model.pho != null)
                strSql.Append("pho='" + model.pho + "',");
            if (model.pho != null)
                strSql.Append("sex='" + model.sex + "',");
            strSql.Append(" addtime=addtime ");
            strSql.Append(" where [id]=" + model.id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改点击量
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public bool UpdateHits(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE lt_message set hits=hits+1 ");
            strSql.Append("where id= " + id.ToString());
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_message ");
            strSql.Append(" where id=" + id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        public bool Delete(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_message ");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 审核消审操作
        /// </summary>
        /// <param name="ids">ID</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public bool UpdateZT(string ids, string zt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_message set ");
            strSql.Append(" zt = " + zt + "");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 公开取消公开操作
        /// </summary>
        /// <param name="ids">ID</param>
        /// <param name="zt">状态</param>
        /// <returns></returns>
        public bool UpdatePublic(string ids, string zt)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_message set ");
            strSql.Append(" inpublic = " + zt + "");
            strSql.Append(" where [id] in(" + ids + ")");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.message GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [id],[username],[tel],[email],[addr],[title],[content],[addtime],[ip],[rel],[relren],[relip],[reltime],[zt],[hits],[class],[pho],[sex] ");
            strSql.Append(" from lt_message ");
            strSql.Append(" where id=" + id);
            ltModel.Model.message model = new ltModel.Model.message();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.id = id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.username = ds.Tables[0].Rows[0]["username"].ToString();
                model.tel = ds.Tables[0].Rows[0]["tel"].ToString();
                model.email = ds.Tables[0].Rows[0]["email"].ToString();
                model.addr = ds.Tables[0].Rows[0]["addr"].ToString();
                model.title = ds.Tables[0].Rows[0]["title"].ToString();
                model.content = ds.Tables[0].Rows[0]["content"].ToString();
                model.addtime = ds.Tables[0].Rows[0]["addtime"].ToString();
                model.ip = ds.Tables[0].Rows[0]["ip"].ToString();
                model.rel = ds.Tables[0].Rows[0]["rel"].ToString();
                model.relren = ds.Tables[0].Rows[0]["relren"].ToString();
                model.relip = ds.Tables[0].Rows[0]["relip"].ToString();
                model.reltime = ds.Tables[0].Rows[0]["reltime"].ToString();
                if (ds.Tables[0].Rows[0]["zt"].ToString() != "")
                {
                    model.zt = int.Parse(ds.Tables[0].Rows[0]["zt"].ToString());
                }
                if (ds.Tables[0].Rows[0]["hits"].ToString() != "")
                {
                    model.hits = int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
                }
                model.Class = ds.Tables[0].Rows[0]["class"].ToString();
                model.pho = ds.Tables[0].Rows[0]["pho"].ToString();
                model.sex = ds.Tables[0].Rows[0]["sex"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],[username],[tel],[email],[addr],[title],[content],[addtime],[ip],[rel],[relren],[relip],[reltime],[zt],[hits],[class],[pho],[sex] ");
            strSql.Append(" FROM lt_message ");
            if (strWhere.Trim() != "")
            {

                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// <param name="TopNum">记录数</param>
        /// <param name="strWhere">条件</param>
        /// </summary>
        public DataSet GetList(string TopNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (TopNum.Trim() != "" && Int32.Parse(TopNum) > 0)
                strSql.Append(" top " + TopNum + " ");
            strSql.Append(" [id],[username],[tel],[email],[addr],[title],[content],[addtime],[ip],[rel],[relren],[relip],[reltime],[zt],[hits],[class],[pho],[sex] ");
            strSql.Append(" FROM lt_message ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}